//
//  GXBussinessTimeController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/27.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXBussinessTimeController.h"
#import "GXManagerTimeController.h"
#import "BRDatePickerView.h"
@interface GXBussinessTimeController ()
@property (weak, nonatomic) IBOutlet UILabel *openShopTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *closeShopTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *dayLabel;
@property (strong,nonatomic) NSDictionary *markDict;
@property (copy, nonatomic) NSString * daytimeStr;//请求的经营时间
@property (copy, nonatomic) NSString * bussinessStr;//请求的开关店铺时间
@property (copy, nonatomic) NSString * bussinessTimeStr;//选择后拼接开关店时间
@property (assign, nonatomic) BOOL changeType;
@end

@implementation GXBussinessTimeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"营业时间";
    _changeType = NO;
    [self requestBussinessTimeData];
}
- (void)requestBussinessTimeData
{
    NSString * storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    [ShopAuthenticationManager selectShopBussniessTimeWithStoreID:storeId success:^(id responseObject) {
        if (responseObject) {
            [self handleRequestBussinessTimeFromResposeOjbce:responseObject];
        }
    } fail:^(id responseObject) {
        
    }];
}
- (void)handleRequestBussinessTimeFromResposeOjbce:(id)responseObject
{
    NSDictionary *contentDic = [responseObject objectForKey:@"result"];
    self.daytimeStr = [NSString stringWithFormat:@"%@",contentDic[@"businesSige"]];
    self.bussinessStr = [NSString stringWithFormat:@"%@",contentDic[@"businesstime"]];
    if (![NSString isBlankString:self.bussinessStr]) {
        NSArray * businessArr = [self.bussinessStr componentsSeparatedByString:@"-"];
        if (businessArr.count) {
            self.openShopTimeLabel.text  = businessArr.firstObject;
            self.closeShopTimeLabel.text  = businessArr.lastObject;
        }
    }
    if ([NSString isBlankString:self.daytimeStr]) {
        self.dayLabel.text = @"未选择";
        return;
    }
    self.dayLabel.text = [NSString getDayString:self.daytimeStr];
}

- (IBAction)clickManagerTimeBtn:(id)sender {
    GXManagerTimeController * managerVc = [[GXManagerTimeController alloc]init];
    managerVc.dayStr =  self.daytimeStr;
    WeakSelf(weakSelf);
    managerVc.daySelectBlock = ^(NSString * _Nonnull dayStr) {
        weakSelf.dayLabel.text = [NSString getDayString:dayStr];
       weakSelf.changeType = YES;
        weakSelf.daytimeStr = dayStr;
    };
    [self.navigationController pushViewController:managerVc animated:YES];
}
- (IBAction)pressOpenShopTimeBtn:(id)sender {
    [BRDatePickerView showDatePickerWithTitle:@"开店时间" dateType:BRDatePickerModeHM defaultSelValue:@"8:00" resultBlock:^(NSString *selectValue) {
        self.openShopTimeLabel.text = selectValue;
    }];
}
- (IBAction)pressCloseShopTimeBtn:(id)sender {
    [BRDatePickerView showDatePickerWithTitle:@"开店时间" dateType:BRDatePickerModeHM defaultSelValue:@"23:00" resultBlock:^(NSString *selectValue) {
        self.closeShopTimeLabel.text = selectValue;
    }];
}

- (IBAction)pressSaveButton:(id)sender {
    if (![self judgeViewValueNull]) {
        return;
    }
    
    [ShopAuthenticationManager changeShopBussniessTimeWithStoreID:StoreId businesstime:self.bussinessTimeStr businesSige:self.daytimeStr success:^(id responseObject) {
        if (responseObject) {
            if (self.changeBussTimeBlock) {
                self.changeBussTimeBlock(YES);
            }
        }
    } fail:^(id responseObject) {
    
    }];
    _changeType = NO;
}
- (BOOL)judgeViewValueNull
{
    if ([NSString isBlankString:self.dayLabel.text]) {
        [SVProgressHUD showErrorWithStatus:@"请选择经营时间"];
        return NO;
    }
    if ([NSString isBlankString:self.openShopTimeLabel.text]) {
        [SVProgressHUD showErrorWithStatus:@"请选择开店时间"];
        return NO;
    }
    if ([NSString isBlankString:self.closeShopTimeLabel.text]) {
        [SVProgressHUD showErrorWithStatus:@"请选择关店时间"];
        return NO;
    }
    if ([self.openShopTimeLabel.text compare: self.closeShopTimeLabel.text] == NSOrderedDescending) {
        [SVProgressHUD showErrorWithStatus:@"关店时间应大于开店时间"];
        return NO;
    }
    self.bussinessTimeStr = [NSString stringWithFormat:@"%@-%@",self.openShopTimeLabel.text,self.closeShopTimeLabel.text];
    if ((!_changeType)&&[self.bussinessTimeStr isEqualToString:self.bussinessStr]) {
        [SVProgressHUD showErrorWithStatus:@"营业时间未做修改"];
        return NO;
    }
    return YES;
}
@end
